package cn.itsource.pethome.user.controller;

import cn.itsource.pethome.user.contant.WeChatContant;
import cn.itsource.pethome.user.service.IWxChatService;
import cn.itsource.pethome.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/wechat")
public class WeChatController {

    @Autowired
    private IWxChatService wxChatService;

    /**
     * 重定向到 拉起微信二维码的页面
     * @return
     */
    @RequestMapping("/toCode")
    public String toCode(){
        String url = WeChatContant.REQUEST_CODE.replace("APPID", WeChatContant.APPID)
                .replace("REDIRECT_URI", WeChatContant.REDIRECT_URL);
        return "redirect:"+url;
    }

    @RequestMapping("/callBack")
    public String callBack(String code){
        return "redirect:http://localhost:8081/callBack.html?code="+code;
    }

    @RequestMapping("/loginOrBinder")
    @ResponseBody
    public AjaxResult loginOrBinder(@RequestBody Map<String, String> map){
        AjaxResult result = wxChatService.loginOrBinder(map);

        return result;
    }

    @RequestMapping("/binder")
    @ResponseBody
    public AjaxResult binder(@RequestBody Map<String, String> map){
        try {
            Map<String, Object> resuleMap = wxChatService.binder(map);
            return AjaxResult.me().setResultObj(resuleMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }
}
